/**
 * @author 钟一华
 */

//sysID
var sysId;
var menuCode;
//节点类型
var archiveClassNodeTypeList;
//获得当前登陆用户
var loginUserId;
//获得当前登陆用户名
var loginName;
//是否进行复制操作
var isCopyFlag = 0;
//复制的节点id
var isCopyId;
var sortNumber;
var nodeId = "";
var newNodeMaxTableName = "";
var nodeData;
//初始化树结构和通用代码,隐藏树结构对应数据栏
$(document).ready(function () {
    loadTreeModel();
    loadLookupList();
    $('#treeDetail').css('display', 'none');
    $('#searchNodeName').searchbox({
        width: 210,
        searcher: function (value) {
            value = encodeURI(value);
            $('#treeModel').tree({
                checkbox: false,
                lines: true,
                dnd: false,
                method: 'post',
                url: 'platform/archiveclassmanagerController/searchTreeNode?value=' + value,
                dataType: 'json',
                loadFilter: function (data) {
                    if (data.data) {
                        return data.data;
                    } else {
                        return data;
                    }
                },
                formatter: function (node) {
                    return '<span title="' + node.text + '">' + node.text + '</span>';
                }
            });
        }
    });
});

//初始化通用代码
function loadLookupList() {
    $.ajax({
        url: 'platform/archiveclassmanagerController/getAllLookupList',
        data: {},
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            if (data.archiveClassNodeTypeList) {
                archiveClassNodeTypeList = data.archiveClassNodeTypeList;
            }
            if (data.loginUserId) {
                loginUserId = data.loginUserId;
            }
            if (data.loginName) {
                loginName = data.loginName;
            }
        }
    });
}
var expandFlag = false;
var firstLoadFlag = true;
//初始化树
function loadTreeModel() {
    $('#treeModel').tree({
        checkbox: false,
        lines: true,
        dnd: false,
        method: 'post',
        url: 'platform/archiveclassmanagerController/findChildsByNodeId',
        dataType: 'json',
        onClick: function (node) {
            if (node) {
                toLoadData(node);
                var acnt = node.attributes.ARCHIVE_CLASS_NODE_TYPE;//是否是文件夹 ，1文件夹
                var at = node.attributes.ARCHIVE_TYPE;
                var pi = node.attributes.PARENT_ID;
                if (acnt == 2) {
                    $('#addTree').linkbutton('disable');
                    $('#delTree').linkbutton('enable');
                    $('#copyTree').linkbutton('enable');
                    $('#pasteTree').linkbutton('disable');
                    $('#sortTree').linkbutton('disable');
                    $("#move").show();
                }
                if (acnt == 1) {
                    $('#addTree').linkbutton('enable');
                    $('#delTree').linkbutton('enable');
                    $('#copyTree').linkbutton('enable');
                    $('#pasteTree').linkbutton('enable');
                    $('#sortTree').linkbutton('enable');
                    $("#move").hide();
                }
                if (acnt == null || acnt == "null") {
                    $('#addTree').linkbutton('enable');
                    $('#delTree').linkbutton('disable');
                    $('#copyTree').linkbutton('disable');
                    $('#pasteTree').linkbutton('enable');
                    $('#sortTree').linkbutton('enable');
                    $("#move").hide();
                }
                if (pi == "ROOT" || at == 0 && acnt == null) {
                    $('#addTree').linkbutton('disable');
                    $('#delTree').linkbutton('disable');
                    $('#copyTree').linkbutton('disable');
                    $('#pasteTree').linkbutton('disable');
                    $('#sortTree').linkbutton('enable');
                    $("#move").hide();
                }
            }
        },
        onSelect: function (node) {
            if (node) {
                tree=node;
                toLoadData(node);
            }
        },
        loadFilter: function (data) {
            if (data.data) {
                return data.data;
            } else {
                return data;
            }
        },
        /*onContextMenu: function(e, node) {
            if (node.id == "newNode") {
                return;
            }
            //如果archiveClassNodeType为2 则不显示新建右键菜单
            var acnt = node.attributes.ARCHIVE_CLASS_NODE_TYPE;
            var at = node.attributes.ARCHIVE_TYPE;
            var pi = node.attributes.PARENT_ID;
            var add = $('#treeMenu').menu('findItem', commonLang.tip.add);
            var del = $('#treeMenu').menu('findItem', commonLang.tip.del);
            var copy = $('#treeMenu').menu('findItem', commonLang.tip.copy);
            var paste = $('#treeMenu').menu('findItem', commonLang.tip.paste);
            var order = $('#treeMenu').menu('findItem', commonLang.tip.sort);
            if (acnt == 2) {
                $('#treeMenu').menu('disableItem', add.target);
                $('#treeMenu').menu('enableItem', del.target);
                $('#treeMenu').menu('enableItem', copy.target);
                $('#treeMenu').menu('disableItem', paste.target);
                $('#treeMenu').menu('disableItem', order.target);
            }
            if (acnt == 1) {
                $('#treeMenu').menu('enableItem', add.target);
                $('#treeMenu').menu('enableItem', del.target);
                $('#treeMenu').menu('enableItem', copy.target);
                $('#treeMenu').menu('enableItem', paste.target);
                $('#treeMenu').menu('enableItem', order.target);
            }
            if (acnt == null || acnt == "null") {
                $('#treeMenu').menu('enableItem', add.target);
                $('#treeMenu').menu('disableItem', del.target);
                $('#treeMenu').menu('disableItem', copy.target);
                $('#treeMenu').menu('enableItem', paste.target);
                $('#treeMenu').menu('enableItem', order.target);
            }
            if (pi == "ROOT" || at == 0 && acnt == null) {
                $('#treeMenu').menu('disableItem', add.target);
                $('#treeMenu').menu('disableItem', del.target);
                $('#treeMenu').menu('disableItem', copy.target);
                $('#treeMenu').menu('disableItem', paste.target);
                $('#treeMenu').menu('enableItem', order.target);
            }
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {
                    parentId: node.id,
                    page: 1,
                    rows: 5
                },
                url: 'platform/archiveclassmanagerController/getTreeDataByParentId',
                success: function(result) {
                    if (result.rows.length > 0) {
                        $('#treeMenu').menu('enableItem', order.target);
                    } else {
                        $('#treeMenu').menu('disableItem', order.target);
                    }
                }
            });
            //树节点右击菜单
            e.preventDefault();
            $(this).tree('select', node.target);
            $('#treeMenu').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        },*/
        onLoadSuccess: function (node, data) {
            if (null != node) {
                if ("" != nodeId) {
                    var findNode = $('#treeModel').tree('find', nodeId);
                    if (null != findNode && "" != findNode) {
                        $('#treeModel').tree('select', findNode.target);
                        toLoadData(findNode);
                        nodeId = "";
                    }
                } else {
                    var root = $('#treeModel').tree('getRoot');
                    $('#treeModel').tree('select', root.target);
                    toLoadData(root);
                }
            }

            if(firstLoadFlag && parent.location.pathname.indexOf('NavigationOfCreateSubOrg.jsp')>0 && data){
                var t = $(this);
                if(expandFlag && data[0]){
                    if(data[0].state == "closed"){
                        t.tree('expand',  $('#treeModel').tree('find',data[0].id).target);
                    }else{
                        $('#treeModel').tree('select',$('#treeModel').tree('find',data[0].id).target);
                        firstLoadFlag = false;
                    }
                }else{
                    $(data).each(function (index, d) {
                        if (this.state == 'closed') {
                            if(d.attributes.PARENT_ID=="ROOT"){
                                t.tree('expand',  $('#treeModel').tree('find',d.id).target);
                            }
                            if(d.text == "档案管理"){
                                t.tree('expand',  $('#treeModel').tree('find',d.id).target);
                                expandFlag = true;
                            }
                        }
                    });
                }
            }
        },
        formatter: function (node) {
            return '<span title="' + node.text + '">' + node.text + '</span>';
        }
    });
}

//加载树节点对应数据
function toLoadData(node) {
    var id = node.id;
    nodeData = node;
    if (id != null && id != "newNode") {
        //判断是否点击的根节点
        if (node.attributes.PARENT_ID == 'ROOT') {
            $('#treeDetail').css('display', 'none');
            return;
        }
        var rootNode = getTypeById(node);
        var classNodeType = node.attributes.ARCHIVE_CLASS_NODE_TYPE;//文件夹还是数据节点
        var rootArchiveType = rootNode.attributes.ARCHIVE_TYPE;//节点类型
        if (rootArchiveType == "1" && classNodeType == "2") {
            /*$('#onlyDeptSeeTh').css('display','inline-table');
            $('#onlyDeptSeeTd').css('display','inline-table');*/
            $('#onlyDeptSeeTh').css('display', 'table-cell');
            $('#onlyDeptSeeTd').css('display', 'table-cell');
        } else {
            $('#onlyDeptSeeTh').css('display', 'none');
            $('#onlyDeptSeeTd').css('display', 'none');
        }
        $('#treeDetail').css('display', 'block');
        $('#treeDetailForm').find('input, textarea, radio').attr('readonly', true);
        if (null == node.attributes.ARCHIVE_CLASS_NODE_TYPE || "" == node.attributes.ARCHIVE_CLASS_NODE_TYPE) {
            $('#nodeSortNumber').attr('readonly', false);
            $('#remark').attr('readonly', false);
        } else {
            $('#archiveClassNodeName').attr('readonly', false);
            $('#nodeSortNumber').attr('readonly', false);
            $('#remark').attr('readonly', false);
            $('#addFormButtons').css('display', 'block');
        }

        //节点名称
        $('#archiveClassNodeName').val(node.attributes.ARCHIVE_CLASS_NODE_NAME);
        //节点顺序
        $('#nodeSortNumber').val(node.attributes.NODE_SORT_NUMBER);
        var archiveClassNodePath = "";
        var createdByName = "";
        var maxTableName = "";
        //获得文字节点路径
        $.ajax({
            type: 'post',
            data: {
                id: node.id,
                sysId: sysId,
                createBy: node.attributes.CREATED_BY,
                path: node.attributes.ARCHIVE_CLASS_NODE_PATH
            },
            url: 'platform/archiveclassmanagerController/getArchiveClassNodePathNameMaxTableName',
            dataType: 'json',
            success: function (result) {
                if (result.flag == 0) {
                    //节点路径
                    //$('#archiveClassNodePath').val(result.pathName);
                    archiveClassNodePath = result.pathName;
                    //创建人
                    //$('#createdByName').val(result.createByName);
                    createdByName = result.createByName;
                    //最大表名
                    //$('#maxTableName').val(result.maxTableName);
                    maxTableName = result.maxTableName;

                    //仅部门可见
                    var attribute01 = result.attribute01;
                    //添加radio
                    var t = "";
                    var n = 1;
                    for (var i = 0; i < archiveClassNodeTypeList.length; i++) {
                        if (node.attributes.ARCHIVE_CLASS_NODE_TYPE == archiveClassNodeTypeList[i].lookupCode) {
                            t += "<input checked=true disabled='disabled' class='radiobox disabled' type='radio' id='archiveClassNodeType" + n + "' name='archiveClassNodeType' value='" + archiveClassNodeTypeList[i].lookupCode + "' /><span class='radiobox-text'>" + archiveClassNodeTypeList[i].lookupName + "</span>";
                        } else {
                            t += "<input disabled='disabled' class='radiobox disabled' type='radio' id='archiveClassNodeType" + n + "' name='archiveClassNodeType' value='" + archiveClassNodeTypeList[i].lookupCode + "' /><span class='radiobox-text'>" + archiveClassNodeTypeList[i].lookupName + "</span>";
                        }
                        n++;
                    }
                    $('#archiveClassNodeType').html(t);
                    if (attribute01 == "1") {
                        $("#onlyDeptSee").prop("checked", true);
                    } else {
                        $("#onlyDeptSee").prop("checked", false);
                    }
                    //格式化日期
                    var date = formatDatebox(node.attributes.CREATION_DATE);
                    $('#treeDetailForm').form('load', {
                        id: node.id,
                        archiveClassNodeName: node.attributes.ARCHIVE_CLASS_NODE_NAME,
                        nodeSortNumber: node.attributes.NODE_SORT_NUMBER,
                        archiveClassNodePath: archiveClassNodePath,
                        createdByName: createdByName,
                        maxTableName: maxTableName,
                        creationDate: date,
                        remark: node.attributes.REMARK,
                        attribute01: attribute01
                    });
                }
            }
        });

        //备注
        //$('#remark').val(node.attributes.REMARK);
        //流程名称
        //$('#').val(node.attributes.RELAT_WORKFLOW);
        //创建时间
        //$('#creationDate').val(date);
        /*
        //给form读取数据
        $('#treeDetailForm').form('load', result.lantopArchiveClass);
        */
    } else {
        $('#addFormButtons').css('display', 'block');
        $('#treeDetailForm').find('input, textarea, radio').attr('readonly', false);
        $('#createdByName').attr('readonly', true);
        $('#creationDate').attr('readonly', true);
        $('#archiveClassNodePath').attr('readonly', true);
        $('#maxTableName').attr('readonly', true);
        $('#relatWorkflow').attr('readonly', true);
        $('#id').attr('readonly', true);
        //初始化默认值
        $('#treeDetailForm').form('clear');
        var pnode = $('#treeModel').tree('getParent', node.target);
        var parentId = pnode.id;
        document.getElementById("parentId").value = parentId;
        $('#nodeSortNumber').val(sortNumber);
        //最大表名
        $('#maxTableName').val(newNodeMaxTableName);
        document.getElementById("archiveClassNodeType1").checked = false;
        document.getElementById("archiveClassNodeType2").checked = true;
        document.getElementById("archiveClassNodeType1").disabled = false;
        document.getElementById("archiveClassNodeType2").disabled = false;
        document.getElementById("createdByName").value = loginName;
        var loginDate = new Date();
        loginDate = formatDatebox(loginDate);
        document.getElementById("creationDate").value = loginDate;
        $('#treeDetail').css('display', 'block');

        var archiveType = pnode.attributes.ARCHIVE_TYPE;//节点类型
        if (archiveType == "1") {
            //仅部门可见设置框可见
            /*$('#onlyDeptSeeTh').css('display','inline-table');
            $('#onlyDeptSeeTd').css('display','inline-table');*/
            $('#onlyDeptSeeTh').css('display', 'table-cell');
            $('#onlyDeptSeeTd').css('display', 'table-cell');
            $('input[type=radio][name=archiveClassNodeType]').change(function () {
                if (this.value == '1') {
                    //仅部门可见设置框可见
                    $('#onlyDeptSeeTh').css('display', 'none');
                    $('#onlyDeptSeeTd').css('display', 'none');
                } else if (this.value == '2') {
                    //仅部门可见设置框可见
                    /*$('#onlyDeptSeeTh').css('display','inline-table');
                    $('#onlyDeptSeeTd').css('display','inline-table');*/
                    $('#onlyDeptSeeTh').css('display', 'table-cell');
                    $('#onlyDeptSeeTd').css('display', 'table-cell');
                }
            });
        }

    }
}

function closeWindow() {
    $('#movedialog').window('close');
}

function saveMove() {
    var targetNode = $('#treeModel1').tree("getSelected");
    //var tree = $('#treeModel').tree("getSelected");

    if (targetNode == null || targetNode == undefined) {
        $.messager.alert(commonLang.tip.alert, "请选择迁移的目标节点!", 'warning');
        return;
    }
    var targetNodeId = targetNode.attributes.ID;
    var treeId = tree.attributes.ID;
    $.ajax({
        type: 'POST',
        url: "platform/archiveclassmanagerController/moveNode",
        data: {
            targetNodeId: targetNodeId,
            treeId: treeId
        },
        dataType: "json",
        success: function (data) {
            if ( data.msg!=null&&data.msg!=""&&data.msg!=undefined){
                $.messager.alert(commonLang.tip.alert, "请选择迁移的目标节点!", 'warning');
            }else {
                $('#treeModel').tree("reload")

                window.location.href=window.location.href;
            }
        }
    });
}

var tree;
//迁移节点
function move() {
    tree = $('#treeModel').tree("getSelected");
    if (tree == null || tree == undefined) {
        $.messager.alert(commonLang.tip.alert, "请选择迁移的节点!", 'warning');
        return;
    }
    // moveNode = tree;
    // var parentNode = $('#treeModel').tree("getParent", tree.target);

    $('#treeModel1').tree({
        checkbox: false,
        lines: true,
        dnd: false,
        method: 'post',
        url: 'platform/archiveclassmanagerController/findChildsByNodeId?id=' + tree.id,
        dataType: 'json',
        loadFilter: function (data) {
            if (data.data) {
                    return  data.data;
                } else {
                    return  data;
                }
            // var resultData;
            // if (data.data) {
            //     resultData = data.data;
            // } else {
            //     resultData = data;
            // }
            //
            // var resultData1 = [];
            // if (resultData != null && resultData != undefined && resultData != "") {
            //     for (var i = 0; i < resultData.length; i++) {
            //         var acnt = resultData[i].attributes.ARCHIVE_CLASS_NODE_TYPE;
            //         if (acnt != 2) {
            //             resultData1.push(resultData[i]);
            //         }
            //     }
            // }
            // parentNode.children = resultData1;
            // return [parentNode];
        },
        formatter: function (node) {
            return '<span title="' + node.text + '">' + node.text + '</span>';
        }
    });


    $('#movedialog').window('open');

}


//新建
function addTreeData() {
    $('#addFormButtons').css('display', 'block');
    $('#treeDetailForm').find('input, textarea, radio').attr('readonly', false);
    $('#createdByName').attr('readonly', true);
    $('#creationDate').attr('readonly', true);
    $('#archiveClassNodePath').attr('readonly', true);
    $('#maxTableName').attr('readonly', true);
    $('#relatWorkflow').attr('readonly', true);
    $('#id').attr('readonly', true);
    var checkNode = $('#treeModel').tree('find', 'newNode');
    if (checkNode) {
        //commonLang.tip.alert替换'提示',commonLang.optip.add.newNodeNoSave替换'新节点未保存,请保存后再创建新节点！'
        $.messager.alert(commonLang.tip.alert, commonLang.optip.add.newNodeNoSave, 'warning');
        return;
    }
    //获得选中节点
    node = nodeData;
    //var node = $('#treeModel').tree('getSelected');
    var parentId = node.id;
    //添加radio
    var t = "";
    var n = 1;
    for (var i = 0; i < archiveClassNodeTypeList.length; i++) {
        if (node.attributes.ARCHIVE_CLASS_NODE_TYPE == archiveClassNodeTypeList[i].lookupCode) {
            t += "<input checked=true class='radiobox' type='radio' id='archiveClassNodeType" + n + "' name='archiveClassNodeType' value='" + archiveClassNodeTypeList[i].lookupCode + "' /><span class='radiobox-text'>" + archiveClassNodeTypeList[i].lookupName + "</span>";
        } else {
            t += "<input  class='radiobox' type='radio' id='archiveClassNodeType" + n + "' name='archiveClassNodeType' value='" + archiveClassNodeTypeList[i].lookupCode + "' /><span class='radiobox-text'>" + archiveClassNodeTypeList[i].lookupName + "</span>";
        }
        n++;
    }
    $('#archiveClassNodeType').html(t);
    $.ajax({
        type: 'post',
        data: {
            id: node.id,
            sysId: sysId,
            createBy: node.attributes.CREATED_BY,
            path: node.attributes.ARCHIVE_CLASS_NODE_PATH
        },
        url: 'platform/archiveclassmanagerController/getArchiveClassNodePathNameMaxTableName',
        dataType: 'json',
        success: function (result) {
            if (result.flag == 0) {
                newNodeMaxTableName = result.maxTableName;
                //最大表名
                $('#maxTableName').val(result.maxTableName);
            }
        }
    });

    //展开选中节点
    $('#treeModel').tree('expand', node.target);
    //判断节点是否存在
    if (node) {
        //添加子节点
        $('#treeModel').tree('append', {
            parent: node.target,
            data: [{
                id: 'newNode',
                text: '节点名称'
            }]
        });
    }
    $('#treeDetailForm').form('clear');
    //设置默认值
    document.getElementById("archiveClassNodeType1").checked = false;
    document.getElementById("archiveClassNodeType2").checked = true;
    document.getElementById("archiveClassNodeType1").disabled = false;
    document.getElementById("archiveClassNodeType2").disabled = false;
    document.getElementById("createdByName").value = loginName;
    document.getElementById("parentId").value = parentId;
    var loginDate = new Date();
    loginDate = formatDatebox(loginDate);
    document.getElementById("creationDate").value = loginDate;
    //获得当前节点最大节点排序值
    $.ajax({
        type: 'post',
        data: {id: node.id},
        url: 'platform/archiveclassmanagerController/getMaxaNodeSortNumber',
        dataType: 'json',
        success: function (result) {
            if (result) {
                sortNumber = result.maxNumber + 1;
                $('#treeDetail').form('load', {
                    nodeSortNumber: result.maxNumber + 1
                });
                //将排序值赋值到value中
                //$('#nodeSortNumber').val(result.maxNumber + 1);
                //获得新子节点
                var newNode = $('#treeModel').tree('find', 'newNode');
                //选中新子节点
                $('#treeModel').tree('select', newNode.target);
            }
        }
    });
    $('#treeDetail').css('display', 'block');
}

//删除
function delTreeData() {
    node = nodeData;
    var tree = $('#treeModel').tree("getSelected");
    //var node = $('#treeModel').tree('getSelected');
    var parentId = node.attributes.PARENT_ID;
    if (parentId == "ROOT") {
        //commonLang.tip.alert替换"提示",commonLang.optip.del.rootNodeNoDel替换"根节点不可以删除！"
        $.messager.alert(commonLang.tip.alert, commonLang.optip.del.rootNodeNoDel, "warning");
        return;
    }
    if (tree!=null&&tree.id!="") {
        //commonLang.tip.confirm替换"确认框",commonLang.optip.del.confirmDelete替换"您确认要删除吗?"
        $.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.confirmDelete, function (r) {
            if (r) {
                var id = tree.id;
                var target = tree.target;
                $.ajax({
                    cache: true,
                    type: "post",
                    url: 'platform/archiveclassmanagerController/checkTreeNodeIsExistData',
                    data: {
                        nodeId: id,
                        sysId: sysId
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.isExist) {
                            //commonLang.tip.confirm替换"确认框",commonLang.optip.del.nodeExistData替换"所选节点下已存在数据，删除后无法恢复，是否确认删除？"
                            $.messager.confirm(commonLang.tip.confirm, commonLang.optip.del.nodeExistData, function (r) {
                                if (r) {
                                    $.ajax({
                                        cache: true,
                                        type: "post",
                                        url: 'platform/archiveclassmanagerController/removeTreeNode',
                                        data: {
                                            id: id,
                                            sysId: sysId,
                                            menuCode: menuCode
                                        },
                                        timeout: 10000,
                                        dataType: "json",
                                        success: function (data) {
                                            if (data.result == 0) {
                                            } else {
                                                parent.parent.parent.$.messager.show({
                                                    title: '消息',
                                                    //commonLang.optip.del.fail替换'删除失败!'
                                                    msg: commonLang.optip.del.fail,
                                                    timeout: 3000,
                                                    showType: 'slide'
                                                });
                                            }
                                        }
                                    });
                                    var node = $('#treeModel').tree('getSelected');
                                    if (node) {
                                        $('#treeModel').tree('remove', target);
                                        $('#treeDetail').css('display', 'none');
                                    }
                                    parent.parent.parent.$.messager.show({
                                        title: '消息',
                                        //commonLang.optip.del.success替换'删除成功!'
                                        msg: commonLang.optip.del.success,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            });
                        } else {
                            $.ajax({
                                cache: true,
                                type: "post",
                                url: 'platform/archiveclassmanagerController/removeTreeNode',
                                data: {
                                    id: id,
                                    sysId: sysId,
                                    menuCode: menuCode
                                },
                                timeout: 10000,
                                dataType: "json",
                                success: function (data) {
                                    if (data.result == 0) {
                                        var node = $('#treeModel').tree('getSelected');
                                        if (node) {
                                            $('#treeModel').tree('remove', target);
                                            $('#treeDetail').css('display', 'none');
                                        }
                                        parent.parent.parent.$.messager.show({
                                            title: '消息',
                                            //commonLang.optip.del.success替换'删除成功!'
                                            msg: commonLang.optip.del.success,
                                            timeout: 3000,
                                            showType: 'slide'
                                        });
                                    } else {
                                        parent.parent.parent.$.messager.show({
                                            title: '消息',
                                            //commonLang.optip.del.fail替换'删除失败!'
                                            msg: commonLang.optip.del.fail,
                                            timeout: 3000,
                                            showType: 'slide'
                                        });
                                    }
                                }
                            });
                        }
                    }
                });
            }
        });
    }
}

//保存
//var flag = false;

function saveData() {
    node = nodeData;
    //var node = $('#treeModel').tree("getSelected");
    var validateFlag = $('#treeDetailForm').form('validate');
    /****正则表达式验证*****/
    $.extend($.fn.validatebox.defaults.rules, {
        xy: {
            validator: function (value, param) {
                var jiedianName = $("#archiveClassNodeName").val();
                var regEn = /[`~!@$%^&*+<>?:"{},.\;'[\]]/im,
                    regCn = /[·！￥：；“”‘，|《。》？\\\\【】[\]]/im;
                if (regEn.test(jiedianName) || regCn.test(jiedianName)) {
                    return false;
                } else {
                    return true;
                }
            },
            //commonLang.tip.noSpecialCharacter替换'节点名称不能含有特殊字符'
            message: commonLang.tip.noSpecialCharacter
        }
    });

    if (validateFlag == false) {
        return;
    }
    if (node.id != "newNode") {
        nodeId = node.id;
        var sortNo = $('#nodeSortNumber').val();
        var className = $('#archiveClassNodeName').val();
        var remark = $('#remark').val();
        var attribute01 = "0";//未选中
        if ($('#onlyDeptSee').is(':checked')) {
            attribute01 = "1";//选中
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: 'platform/archiveclassmanagerController/updateData',
            data: {
                id: node.id,
                sortNo: sortNo,
                className: className,
                remark: remark,
                attribute01: attribute01
            },
            success: function (result) {
                var currentNode = $('#treeModel').tree('find', nodeId);
                $('#treeModel').tree('update', {
                    target: currentNode.target,
                    text: className
                });
                if (result.flag == 0) {
                    //$('#treeDetail').css('display','none');
                    parent.parent.parent.$.messager.show({
                        title: '消息',
                        //commonLang.optip.save.success替换'保存成功'
                        msg: commonLang.optip.save.success,
                        timeout: 3000,
                        showType: 'slide'
                    });
                    //var node = $('#treeModel').tree('getRoot');
                    //$('#treeModel').tree('reload', node.target);
                } else {
                    parent.parent.parent.$.messager.show({
                        title: '消息',
                        //commonLang.optip.save.fail替换'保存失败'
                        msg: commonLang.optip.save.fail,
                        timeout: 3000,
                        showType: 'slide'
                    });
                }
            }
        });
    } else {
        var nodeSortNumber = $('#nodeSortNumber').val();
        if ($('#onlyDeptSee').is(':checked')) {
            $("#attribute01").val("1");
        } else {
            $("#attribute01").val("0");
        }
        var dataVo = $('#treeDetailForm').serializeArray();
        var dataJson = convertToJson(dataVo);
        dataVo = JSON.stringify(dataJson);
        $.ajax({
            type: 'post',
            url: 'platform/archiveclassmanagerController/addTreeNode',
            dataType: 'json',
            cache: true,
            data: {
                data: dataVo,
                sysId: sysId,
                sortNumber: nodeSortNumber
            },
            //timeout: 10000,
            success: function (r) {
                nodeId = r.id;
                if (r.flag == 0) {
                    $('#treeDetail').css('display', 'none');
                    parent.parent.parent.$.messager.show({
                        title: '消息',
                        //commonLang.optip.save.success替换'保存成功'
                        msg: commonLang.optip.save.success,
                        timeout: 3000,
                        showType: 'slide'
                    });
                    var node = $('#treeModel').tree('getRoot');
                    $('#treeModel').tree('reload', node.target);
                } else {
                    parent.parent.parent.$.messager.show({
                        title: '消息',
                        msg: r.msg,
                        timeout: 3000,
                        showType: 'slide'
                    });
                }
            }
        });
    }
}

//重置
function resetData() {
    //var node = $('#treeModel').tree('getSelected');
    toLoadData(node);
}

//复制
function copyTreeData() {
    node = nodeData;
    //var node = $('#treeModel').tree('getSelected');
    var parentId = node.attributes.PARENT_ID;
    if (parentId == "ROOT") {
        //commonLang.tip.alert替换"提示",commonLang.optip.rootNodeNoCopy替换"根节点不可以进行复制操作！"
        $.messager.alert(commonLang.tip.alert, commonLang.optip.rootNodeNoCopy, "warning");
        return;
    }
    if (node.id != "newNode") {
        $.ajax({
            type: 'post',
            url: 'platform/archiveclassmanagerController/getArchiveClassById',
            dataType: 'json',
            data: {
                id: node.id
            },
            success: function (result) {
                //获得复制目标id
                isCopyId = result.lantopArchiveClass.id;
                //改变是否复制状态
                isCopyFlag = 1;
            }
        });
    }
}

//粘贴
function pasteTreeData() {
    if (isCopyFlag == 0) {
        //commonLang.tip.alert替换"提示",commonLang.optip.copyFirst替换"请先进行复制操作,再做粘贴操作！"
        $.messager.alert(commonLang.tip.alert, commonLang.optip.copyFirst, "warning");
        return;
    } else {
        //var node = $('#treeModel').tree('getSelected');
        node = nodeData;
        if (node.id != "newNode") {
//			$.ajax({
//				type: 'post',
//				url: 'platform/archiveclassmanagerController/isRepeatNode',
//				dataType: 'json',
//				data: {
//					parentId: node.id,
//					copyId: isCopyId
//				},
//				success: function(r) {
//					if (r.isRepeat == false) {
            $.ajax({
                type: 'post',
                url: 'platform/archiveclassmanagerController/pasteTreeData',
                dataType: 'json',
                data: {
                    parentId: node.id,
                    copyId: isCopyId,
                    sysId: sysId
                },
                success: function (result) {
                    //清空复制目标id
                    isCopyId = "";
                    //改变是否复制状态
                    isCopyFlag = 0;
                    var root = $('#treeModel').tree('getRoot');
                    $('#treeModel').tree('reload', root.target);
                }
            });
//					} else {
//						$.messager.alert("提示", "目标节点下存在相同的节点数据,请勿重复操作!", "warning");
//						return;
//					}
//				}
//			});
        }
    }
}

//排序
function orderTreeData() {
    node = nodeData;
    //var node = $('#treeModel').tree('getSelected');
    var dialogId = "formOrderDialog";
    var url = "platform/archiveclassmanagerController/toOrderJsp?dialogId=" + dialogId + "&id=" + node.id + "&sysId=" + sysId;
    //commonLang.tip.sort替换"排序"
    var formOrder = new CommonDialog(dialogId, "800", "400", url, commonLang.tip.sort, false, true, false);
    formOrder.show();
}

function reloadTree(id) {
    nodeId = id;
    var node = $('#treeModel').tree('getRoot');
    $('#treeModel').tree('reload', node.target);
}

//树查询
function searchTree() {
    var value = $('#searchNodeName').val();
    value = encodeURI(value);
    $('#treeModel').tree({
        checkbox: false,
        lines: true,
        dnd: false,
        method: 'post',
        url: 'platform/archiveclassmanagerController/searchTreeNode?value=' + value,
        dataType: 'json',
        loadFilter: function (data) {
            if (data.data) {
                return data.data;
            } else {
                return data;
            }
        },
        formatter: function (node) {
            return '<span title="' + node.text + '">' + node.text + '</span>';
        }
    });
}

//回车事件
function keyDownSearch() {
    if (event.keyCode == 13) {
        searchTree();
    }
}

//根据当前节点获得其数据类型
function getTypeById(node) {
    var archiveType = node.attributes.ARCHIVE_TYPE;
    if (archiveType != null && archiveType != "") {
        return node;
    }
    var parentId = node.attributes.PARENT_ID;
    var parent_node = $('#treeModel').tree('find', parentId);
    return getTypeById(parent_node);
}